package com.oocl.admin_platform.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;







import java.util.logging.LogManager;

import org.apache.log4j.Logger;

import com.oocl.admin_platform.dao.MerchantDao;
import com.oocl.admin_platform.db.factory.DBConnectionFactory;
import com.oocl.admin_platform.pojo.Merchant;
import com.oocl.admin_platform.util.DBUtil;

import com.oocl.admin_platform.util.UUIDEasyUtil;




public class MerchantDaoImpl implements MerchantDao {

	private static Logger logger = Logger.getLogger(MerchantDaoImpl.class);
	
	
	
	@Override
	public Merchant findByuname(String uname) {
		// TODO Auto-generated method stub
		String sql = "select * from merchant where uname=?";
		logger.info("enter findByuname method uname:   "+uname);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Merchant merchant=null;
		connection=DBConnectionFactory.getInstance();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, uname);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				merchant=new Merchant();
				merchant.setId(resultSet.getString("id"));
				merchant.setUname(resultSet.getString("uname"));
				merchant.setAddress(resultSet.getString("address"));
				merchant.setIdCard(resultSet.getBytes("idCard"));
				merchant.setPortrait(resultSet.getBytes("portrait"));
				merchant.setStatus(resultSet.getInt("status"));
			}
		} catch (SQLException e) {
			logger.error("fail on findByuname  uname "+uname+"  "+e.getMessage());
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return merchant;
	}

	@Override
	public List<Merchant> findByStatus(int status) {
		// TODO Auto-generated method stub
		List<Merchant> merchatList = new ArrayList<Merchant>();
		logger.info("enter findByStatus method status:  "+status);
		String sql = "select * from merchant where status=?";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		//connection = DBUtil.getConnection();
		connection=DBConnectionFactory.getInstance();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, status);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Merchant merchant=new Merchant();
				merchant.setId(resultSet.getString("id"));
				merchant.setUname(resultSet.getString("uname"));
				merchant.setAddress(resultSet.getString("address"));
				merchant.setIdCard(resultSet.getBytes("idCard"));
				merchant.setPortrait(resultSet.getBytes("portrait"));
				merchant.setStatus(resultSet.getInt("status"));
				merchatList.add(merchant);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("fail on findByStatus  status "+status+"  "+e.getMessage());
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return merchatList;
	}
	
	
	@Override
	public boolean save(Merchant merchant) {
		// TODO Auto-generated method stub
		String sql = "insert into MERCHANT(id,uname,IdCard,portrait,address,status) values(?,?,?,?,?,0)";
		logger.info("enter save method merchant:  "+merchant);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		connection=DBConnectionFactory.getInstance();
		int m = 0;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, merchant.getId());
			preparedStatement.setString(2, merchant.getUname());
			preparedStatement.setBytes(3, merchant.getIdCard());
			preparedStatement.setBytes(4, merchant.getPortrait());
			preparedStatement.setString(5, merchant.getAddress());
			m = preparedStatement.executeUpdate();
			
			if (m>0) {
				logger.info("save success:   "+merchant);
				return true;
			}
			else{
				logger.info("save fail:   "+merchant);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("save fail:   "+merchant);
			
		} finally {
			DBUtil.close(connection, preparedStatement, null);
		}
		return false;
	}

	@Override
	public boolean update(Merchant merchant) {
		// TODO Auto-generated method stub
		String sql="update Merchant set IDCard=?,portrait=?,address=?,status=? where uname=?";
		logger.info("enter the MerchantDao update method "+merchant);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		//connection = DBUtil.getConnection();
		connection=DBConnectionFactory.getInstance();
		int m = 0;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setBytes(1, merchant.getIdCard());
			preparedStatement.setBytes(2, merchant.getPortrait());
			preparedStatement.setString(3, merchant.getAddress());
			preparedStatement.setInt(4, merchant.getStatus());
			preparedStatement.setString(5, merchant.getUname());
			m = preparedStatement.executeUpdate();
			if (m>0) {
				logger.info("update success in update method "+merchant);
				return true;
			}
			else{
				logger.error("update fail in update method "+merchant);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("fail in update method exception "+e.getMessage());
//			e.printStackTrace();
			
		} finally {
			DBUtil.close(connection, preparedStatement, null);
		}
		return false;
	}

	@Override
	public List<Merchant> findAll() {
		// TODO Auto-generated method stub
		List<Merchant> merchatList = new ArrayList<Merchant>();
		String sql = "select * from merchant";
		logger.info("enter the findAll method ");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		//connection = DBUtil.getConnection();
		connection=DBConnectionFactory.getInstance();
		try {
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Merchant merchant=new Merchant();
				merchant.setId(resultSet.getString("id"));
				merchant.setUname(resultSet.getString("uname"));
				merchant.setAddress(resultSet.getString("address"));
				merchant.setIdCard(resultSet.getBytes("idCard"));
				merchant.setPortrait(resultSet.getBytes("portrait"));
				merchant.setStatus(resultSet.getInt("status"));
				merchatList.add(merchant);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("fail in findAll method exception "+e.getMessage());
//			e.printStackTrace();
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return merchatList;
	}

	@Override
	public Merchant findById(String id) {
		// TODO Auto-generated method stub
		String sql = "select * from merchant where id=?";
		logger.info("enter the findById method id= "+id);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Merchant merchant=null;
		connection=DBConnectionFactory.getInstance();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, id);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				merchant=new Merchant();
				merchant.setId(resultSet.getString("id"));
				merchant.setUname(resultSet.getString("uname"));
				merchant.setAddress(resultSet.getString("address"));
				merchant.setIdCard(resultSet.getBytes("idCard"));
				merchant.setPortrait(resultSet.getBytes("portrait"));
				merchant.setStatus(resultSet.getInt("status"));
			}
		} catch (SQLException e) {
			logger.error("fail in findById method id "+id);
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return merchant;
	}

}
